package org.arain.baseii.common;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.arain.baseii.upload.UploadPojo;
import org.arain.baseii.upload.UploadResult;
import org.arain.baseii.upload.UploadService;
import org.arain.baseii.utils.AppUtils;
import org.arain.baseii.utils.ArainResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Controller
public class CommonController {
	@Autowired
	private UploadService uploadService;
	
	@RequestMapping("system/error.do")
	public String reload_error() {
		return "common/error";
	}
	
	@RequestMapping("/system/404")
	public String reload_404() {
		return "error/404";
	}
	
	@RequestMapping(value="system/common/load_enabled.do",produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String load_enabled() {
		JSONObject object = new JSONObject();
		JSONArray array = new JSONArray();
		object.put("id", 1);
		object.put("text", "是");
		
		JSONObject object1 = new JSONObject();
		object1.put("id", 0);
		object1.put("text", "否");
		
		array.add(object1);
		array.add(object);
		return array.toString();
	}
	
	
	@RequestMapping(value="/system/common/load_log_state.do",produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String load_log_state() {
		JSONObject object = new JSONObject();
		JSONArray array = new JSONArray();
		object.put("id", 1);
		object.put("text", "成功");
		
		JSONObject object1 = new JSONObject();
		object1.put("id", 0);
		object1.put("text", "失败");
		
//		JSONObject object3 = new JSONObject();
//		object3.put("id","");
//		object3.put("text",null);
//		array.add(object3);
		array.add(object1);
		array.add(object);
		return array.toString();
	}
	
	@RequestMapping(value="/system/common/load_css.do",produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String load_css() {
		JSONObject object = new JSONObject();
		JSONArray array = new JSONArray();
		object.put("id","insdep_theme_default.css");
		object.put("text", "星空黑");
		
		JSONObject object1 = new JSONObject();
		object1.put("id", "insdep_theme_red.css");
		object1.put("text", "烈焰红");
		
		JSONObject object3 = new JSONObject();
		object3.put("id","insdep_theme_blue.css");
		object3.put("text","水晶蓝");
		JSONObject object4 = new JSONObject();
		object4.put("id","");
		object4.put("text", "选择样式");
		array.add(object4);
		array.add(object3);
		array.add(object1);
		array.add(object);
		return array.toString();
	}
	
	
	@CrossOrigin
	@RequestMapping(value="/common/upload", method = RequestMethod.POST)
	@ResponseBody
	public UploadResult save_01(HttpServletRequest request,UploadPojo pojo,HttpServletResponse response) throws IOException{
		response.setHeader("Access-Control-Allow-Origin", "*"); //生产环境绝对不允许设置为“*”
	    response.setHeader("Access-Control-Allow-Methods", "*");
	    response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
	    response.setContentType("application/json");
	    response.setCharacterEncoding("utf-8");
	    UploadResult result = uploadService.save_01(request, pojo);
		return result;
	}
	
	@RequestMapping(value="/common/upload_check")
	@ResponseBody
	public String upload_check(HttpServletRequest request,UploadPojo pojo,HttpServletResponse response) throws IOException{
		response.setHeader("Access-Control-Allow-Origin", "*"); //生产环境绝对不允许设置为“*”
	    response.setHeader("Access-Control-Allow-Methods", "*");
	    response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
	    response.setContentType("application/json");
	    response.setCharacterEncoding("utf-8");
		String path =Class.class.getClass().getResource("/").getPath();
		String string = AppUtils.ReadFile(path+"config.json");
		return string;
	}
	
	
	public static void main(String[] args) {
		String path =Class.class.getClass().getResource("/").getPath();
		String string = AppUtils.ReadFile(path+"config.json");
//		String toJson = JsonUtils.objectToJson(string);
		System.out.println(string);
	}
	
	@RequestMapping("/common/load_img")
	@ResponseBody
	public ArainResult load_image(Long id) {
		return uploadService.load_img(id);
	}
}
